package main

import (
	"fmt"
	"sync"
	"time"
)

var sum int

// var mutex sync.Mutex
var mutex sync.RWMutex

func main() {
	for i := 0; i < 100; i++ {
		go add(10)
	}

	for i := 0; i < 10; i++ {
		go fmt.Println("sum:", readSum())
	}
	time.Sleep(2 * time.Second)
}

func add(n int) int {
	sum += n
	return sum
}

func readSum() int {
	mutex.Lock()
	defer mutex.Unlock()
	b := sum
	return b
}
